package com.holly.unit.deform.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.holly.unit.core.constants.CommonConstant;
import com.holly.unit.deform.entity.DesignForm;
import com.holly.unit.deform.entity.DesignFormUrlAuth;
import com.holly.unit.deform.mapper.DeformUrlAuthMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 类描述: 表单设计 URL授权 服务类
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/4/11 15:20
 */
@Service
public class DeformUrlAuthService extends ServiceImpl<DeformUrlAuthMapper, DesignFormUrlAuth> {

    @Autowired
    private DeformBaseInfoService designFormService;

    @Transactional(rollbackFor = Exception.class)
    public boolean initialUrlAuth(String desformCode) {
        DesignForm designForm = designFormService.getByCode(desformCode);
        if (designForm != null) {
            String id = designForm.getId();
            String code = designForm.getDesformCode();
            this.baseMapper.insert(new DesignFormUrlAuth(id, code, CommonConstant.DESIGN_FORM_URL_TYPE_ADD));
            this.baseMapper.insert(new DesignFormUrlAuth(id, code, CommonConstant.DESIGN_FORM_URL_TYPE_EDIT));
            this.baseMapper.insert(new DesignFormUrlAuth(id, code, CommonConstant.DESIGN_FORM_URL_TYPE_DETAIL));
            return true;
        }
        return false;
    }

    public boolean urlIsPassed(String desformCode, String urlType) {
        Integer status = this.getUrlStatus(desformCode, urlType);
        return CommonConstant.DESIGN_FORM_URL_STATUS_PASSED.equals(status);
    }

    public Integer getUrlStatus(String desformCode, String urlType) {
        if (this.isTypeNormal(urlType)) {
            LambdaQueryWrapper<DesignFormUrlAuth> queryWrapper = getWrapper(desformCode);
            if (queryWrapper != null) {
                queryWrapper.eq(DesignFormUrlAuth::getUrlType, urlType);
                DesignFormUrlAuth auth = this.baseMapper.selectOne(queryWrapper);
                if (auth == null) {
                    this.initialUrlAuth(desformCode);
                    return CommonConstant.DESIGN_FORM_URL_STATUS_PASSED;
                }
                return auth.getUrlStatus();
            }
        }
        return null;
    }

    public boolean setUrlStatus(String desformCode, String urlType, Integer status) {
        LambdaQueryWrapper<DesignFormUrlAuth> wrapper = getWrapper(desformCode);
        if (wrapper != null) {
            return this.setUrlStatus(wrapper, urlType, status);
        }
        return false;
    }

    public boolean setUrlStatus(LambdaQueryWrapper<DesignFormUrlAuth> wrapper, String urlType, Integer status) {
        DesignFormUrlAuth auth = new DesignFormUrlAuth();
        auth.setUrlStatus(status);

        wrapper.eq(DesignFormUrlAuth::getUrlType, urlType);
        return this.baseMapper.update(auth, wrapper) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean setAllUrlStatus(String desformCode, Integer... status) {
        // 判断 desformCode 是否存在
        if (designFormService.getByCode(desformCode) != null) {
            this.setUrlStatus(getWrapperNormal(desformCode), CommonConstant.DESIGN_FORM_URL_TYPE_ADD, status[0]);
            this.setUrlStatus(getWrapperNormal(desformCode), CommonConstant.DESIGN_FORM_URL_TYPE_EDIT, status[1]);
            this.setUrlStatus(getWrapperNormal(desformCode), CommonConstant.DESIGN_FORM_URL_TYPE_DETAIL, status[2]);
            return true;
        }
        return false;
    }

    /**
     * 判断是否是已识别的type类型
     */
    private boolean isTypeNormal(String urlType) {
        switch (urlType) {
            case CommonConstant.DESIGN_FORM_URL_TYPE_ADD:
            case CommonConstant.DESIGN_FORM_URL_TYPE_EDIT:
            case CommonConstant.DESIGN_FORM_URL_TYPE_DETAIL:
            case CommonConstant.DESIGN_FORM_URL_TYPE_VIEW:
                return true;
            default:
                return false;
        }
    }

    private LambdaQueryWrapper<DesignFormUrlAuth> getWrapper(String desformCode) {
        // 判断 desformCode 是否存在
        if (designFormService.getByCode(desformCode) != null) {
            return this.getWrapperNormal(desformCode);
        }
        return null;
    }
    private LambdaQueryWrapper<DesignFormUrlAuth> getWrapperNormal(String desformCode) {
        LambdaQueryWrapper<DesignFormUrlAuth> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DesignFormUrlAuth::getDesformCode, desformCode);
        return wrapper;
    }

}
